package com.weetop.akly.activity.communicationIndex;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.weetop.akly.R;
import com.weetop.akly.appConfig.GlobalConstants;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BaseObserver;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.bean.DepartmentBean;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.rong.imkit.RongIM;

public class IndexActivity extends BaseActivity {

    @BindView(R.id.rcy_index)
    RecyclerView rcyIndex;
    @BindView(R.id.et_search)
    EditText etSearch;
    private String fromModeId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private ArrayList<DepartmentBean> departmentBeans = new ArrayList<>();
    private CommonRecyclerAdapter<DepartmentBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        ButterKnife.bind(this);
        fromModeId = getIntent().getStringExtra("mode_id");
        initData();
    }

    private void initData() {
        rcyIndex.setItemAnimator(null);
        rcyIndex.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<DepartmentBean>(mActivity, R.layout.item_index, departmentBeans) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, DepartmentBean item, int position) {
                helper.setText(R.id.tv_departName, item.getName() + "(" + item.getCount() + ")");
            }
        };
        rcyIndex.setAdapter(adapter);
        adapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
                if (BaseUtils.fastClick()) {
                    Intent intent = new Intent();
                    intent.putExtra("department_id", departmentBeans.get(position).getId());
                    intent.putExtra("title", departmentBeans.get(position).getName());
                    intent.putExtra("fromModeId", fromModeId);
                    BaseUtils.toActivity(mActivity, DepartmentActivity.class, intent);
                }
            }
        });
        //获取部门列表
        getDepartmentList();
        initEvent();
    }


    private void initEvent() {
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    performSearch();
                    return true;
                }
                return false;
            }
        });
    }

    private void performSearch() {
        etSearch.clearFocus();
        InputMethodManager in = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        in.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
        //...perform search
        String keywords = etSearch.getText().toString().trim();
        if (!TextUtils.isEmpty(keywords)) {
            Intent intent = new Intent();
            intent.putExtra("keywords", keywords);
            intent.putExtra("title", "搜索结果");
            intent.putExtra("fromModeId", fromModeId);
            BaseUtils.toActivity(mActivity, DepartmentActivity.class, intent);
        }
    }

    /**
     * 获取部门列表
     */
    public void getDepartmentList() {
        HashMap<String, String> data = myApplication.getGlobalData();
        addDisposable(apiServer.getDepartmentList(data), new BaseObserver<BaseModel<ArrayList<DepartmentBean>>>(this) {
            @Override
            public void onSuccess(BaseModel<ArrayList<DepartmentBean>> o) {
                departmentBeans.clear();
                departmentBeans.addAll(o.getData());
                adapter.replaceAll(departmentBeans);
            }

            @Override
            public void onError(String msg) {
                showError(msg);
            }
        });
    }


    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }

}
